import * as loginService from '../services/logins';
import {setcookie} from '../utils/cookies'
import {getcookie} from '../utils/cookies'
import {routerRedux} from 'dva/router'
import {EXPIRE_SECONDS} from '../utils/constants'

export default {
  namespace: 'logins',
  state: {},
  reducers: {},
  effects: {
    *mlogin({payload: code},{call,put}){
      const {data} = yield call (loginService.mlogin, code);
      // console.log("电脑登录，后端返回的data",data);

      if (data.success == true) {
        // console.log("---------data.success",data.success)
        setcookie("headImgUrl",data.data.headImgUrl,EXPIRE_SECONDS);
        setcookie("nickname",data.data.nickname,EXPIRE_SECONDS);
        setcookie("role",data.data.role,EXPIRE_SECONDS);
        setcookie("unionid",data.data.unionid,EXPIRE_SECONDS);
        setcookie("status",data.data.status,EXPIRE_SECONDS);
        let headeImg = getcookie("headImgUrl")
        // console.log("-------------headeImg",headeImg)
        yield put(routerRedux.replace({pathname:'/'}))

      } else {
        alert("登录失败，请退出稍后重试")
        // const args = {
        //   message: '退款结果',
        //   description: '只退逸贝，退失败',
        //   duration: 0,
        //   icon: <Icon type="smile-circle" style={{ color: '#108ee9' }} />,
        // };
        // notification.open(args);
      }






      // if(data.status=="101" && data.role == "user"){
      //   //202是管理员(admin)登录
      //   setcookie("headImgUrl",data.headImgUrl,EXPIRE_SECONDS);
      //   setcookie("nickname",data.nickname,EXPIRE_SECONDS);
      //   setcookie("role",data.role,EXPIRE_SECONDS);
      //   setcookie("unionid",data.unionid,EXPIRE_SECONDS);
      //   setcookie("status",data.status,EXPIRE_SECONDS);
      //   if (data.cookieToken != null && data.cookieToken != '') {
      //     setcookie("cookieToken",data.cookieToken,EXPIRE_SECONDS);
      //   }
      //   yield put(routerRedux.replace({pathname:'/'}))
      // }else if(data.status == "202" && data.role == "admin"){
      //   //202是管理员(admin)登录
      //   setcookie("headImgUrl",data.headImgUrl,EXPIRE_SECONDS);
      //   setcookie("nickname",data.nickname,EXPIRE_SECONDS);
      //   setcookie("role",data.role,EXPIRE_SECONDS);
      //   setcookie("unionid",data.unionid,EXPIRE_SECONDS);
      //   setcookie("status",data.status,EXPIRE_SECONDS);
      //   if (data.cookieToken != null && data.cookieToken != '') {
      //     setcookie("cookieToken",data.cookieToken,EXPIRE_SECONDS);
      //   }
      //   yield put(routerRedux.replace({pathname:'/admin'}))
      // } else if(data.status=="203"){
      //   //203表示未关注公众号
      //   alert('还没有成为环球小伙伴')
      // }else if(data.status=="204"){
      //   alert('登录超时')
      // }else if(data.status=="102"){
      //   alert('登录失败')
      // }
    },

    *plogin({payload: code},{call,put}){
      const {data} = yield call (loginService.plogin, code);
      // console.log("电脑登录，后端返回的data",data);
      if(data.status=="101" && data.role == "user"){
        //202是管理员(admin)登录
        setcookie("headImgUrl",data.headImgUrl,EXPIRE_SECONDS);
        setcookie("nickname",data.nickname,EXPIRE_SECONDS);
        setcookie("role",data.role,EXPIRE_SECONDS);
        setcookie("unionid",data.unionid,EXPIRE_SECONDS);
        setcookie("status",data.status,EXPIRE_SECONDS);
        if (data.cookieToken != null && data.cookieToken != '') {
          setcookie("cookieToken",data.cookieToken,EXPIRE_SECONDS);
        }
        yield put(routerRedux.replace({pathname:'/'}))
      }else if(data.status == "202" && data.role == "admin"){
        //202是管理员(admin)登录
        setcookie("headImgUrl",data.headImgUrl,EXPIRE_SECONDS);
        setcookie("nickname",data.nickname,EXPIRE_SECONDS);
        setcookie("role",data.role,EXPIRE_SECONDS);
        setcookie("unionid",data.unionid,EXPIRE_SECONDS);
        setcookie("status",data.status,EXPIRE_SECONDS);
        if (data.cookieToken != null && data.cookieToken != '') {
          setcookie("cookieToken",data.cookieToken,EXPIRE_SECONDS);
        }
        yield put(routerRedux.replace({pathname:'/admin'}))
      } else if(data.status=="203"){
        //203表示未关注公众号
        alert('还没有成为环球小伙伴')
      }else if(data.status=="204"){
        alert('登录超时')
      }else if(data.status=="102"){
        alert('登录失败')
      }
    },
  },
  subscriptions: {},
};
